package DataStrucures;

public class BinarySortTreeDemo {
    public static void main(String[] args) {
        int[]arr={7,3,10,12,5,1,9};
        BinarySortTree binarySortTree=new BinarySortTree();
        for (int i = 0; i < arr.length; i++) {
            binarySortTree.add(new SortNode(arr[i]));
        }
        binarySortTree.infixOrder();
    }
}
class BinarySortTree{
    private SortNode root;
    public void add(SortNode node){
        if(root==null){
            root=node;
        }else {
            root.add(node);
        }
    }
    public void infixOrder(){
        if(root==null){
            System.out.println("空树");
        }else {
            root.infixOrder();
        }
    }
}
class SortNode{
    int value;
    SortNode left;
    SortNode right;

    public SortNode(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "SortNode{" +
                "value=" + value +
                '}';
    }

    public void add(SortNode node){//比本节点小放左边反之放右边
        if(node==null){
            return;
        }
        if(node.value<this.value){
            if(this.left!=null){
                this.left.add(node);
            }
            if(this.left==null){
                this.left=node;
            }
        }else {
            if(this.right!=null){
                this.right.add(node);
            }
            if(this.right==null){
                this.right=node;
            }

        }
    }
    public void infixOrder(){
        if(this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right!=null){
            this.right.infixOrder();
        }
    }

}